//
//  ZFTitleView.swift
//  map
//
//  Created by fyzq on 2024/1/26.
//

import UIKit
import RxSwift
class ZFContentTitleView: ZFBaseView {
    private(set) var titleCell:ZFContentCustomCell!
    private(set) var contentView:UIView!
    override init(frame: CGRect) {
        super.init(frame: frame)
        setZFTitleView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    fileprivate func setZFTitleView(){
    
        titleCell = ZFContentCustomCell()
        titleCell.titleLabel.font = UIFont.boldSystemFont(ofSize: 15)
        titleCell.arrow.isHidden = true

        contentView = UIView()
        contentView.backgroundColor = R.color.bg()
        contentView.addRadius(radius: 6)
        
        let group = ZFGroupCustomCell(arrangedSubviews: [titleCell,contentView])
        addSubview(group)
        group.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
}

/**
 地图设置
 */
class ZFMapSettingView : ZFContentTitleView {
    var subject = PublishSubject<Int>()
    override init(frame: CGRect) {
        super.init(frame: frame)
        setZFMapSettingView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    fileprivate func setZFMapSettingView(){
        self.titleCell.titleLabel.text = "地图设置"
        
        
        let centerView = ZFContentCrossView()
        
        let rotateView = ZFSwitchCustomCell()
        rotateView.titleLabel.text = "允许地图旋转"
        
        let pitchView = ZFSwitchCustomCell()
        pitchView.titleLabel.text = "允许地图3D模式"
        
        let roadView = ZFSwitchCustomCell()
        roadView.titleLabel.text = "显示路网"
        
        let placeView = ZFSwitchCustomCell()
        placeView.titleLabel.text = "显示路标"
        
        let lastView = ZFSwitchCustomCell()
        lastView.titleLabel.text = "回到上次浏览位置"
        
        let elevationView = ZFContentElevationView()
        
        
        
        let mapSettingView = ZFGroupCustomCell(arrangedSubviews: [centerView,rotateView,pitchView,roadView,placeView,lastView,elevationView])
        contentView.addSubview(mapSettingView)
        mapSettingView.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(UIEdgeInsets.init(top: 5, left: 10, bottom: 5, right: 10))
        }
        ///配置信息
        guard let config = ZFMapConfigModal.getMapConfig(),
        let point = config.point else {
            return
        }
        
        centerView.isOn = point.showImage
        rotateView.switchBar.isOn = config.enableRotate;
        pitchView.switchBar.isOn = config.enablePitch;
        roadView.switchBar.isOn = config.enableRoad;
        placeView.switchBar.isOn = config.enablePlace
        lastView.switchBar.isOn = config.enableRecordCoordinate
        elevationView.isOn = config.enableElevation
        
        centerView.switchCell.switchBar.rx.isOn.changed.observe(on: MainScheduler.instance).subscribe(onNext: {[weak self] isOn in
            guard let self = self else {return}
            ZFRealmManager.updateWithTranstion { bo in
                config.point?.showImage = isOn
            }
            self.subject.onNext(0)
        }).disposed(by: dis)
        
        rotateView.switchBar.rx.isOn.changed.observe(on: MainScheduler.instance).subscribe(onNext: {[weak self]isOn in
            guard let self = self else {return}
            ZFRealmManager.updateWithTranstion { bo in
                config.enableRotate = isOn
            }
            self.subject.onNext(1)
        }).disposed(by: dis)
        
        pitchView.switchBar.rx.isOn.changed.observe(on: MainScheduler.instance).subscribe(onNext: {[weak self]isOn in
            guard let self = self else {return}
            ZFRealmManager.updateWithTranstion { bo in
                config.enablePitch = isOn
            }
            self.subject.onNext(2)
        }).disposed(by: dis)
        roadView.switchBar.rx.isOn.changed.observe(on: MainScheduler.instance).subscribe(onNext: {[weak self]isOn in
            guard let self = self else {return}
            ZFRealmManager.updateWithTranstion { bo in
                config.enableRoad = isOn
            }
            self.subject.onNext(3)
        }).disposed(by: dis)
        
        placeView.switchBar.rx.isOn.changed.observe(on: MainScheduler.instance).subscribe(onNext: {[weak self]isOn in
            guard let self = self else {return}
            ZFRealmManager.updateWithTranstion { bo in
                config.enablePlace = isOn
            }
            self.subject.onNext(4)
        }).disposed(by: dis)
        lastView.switchBar.rx.isOn.changed.observe(on: MainScheduler.instance).subscribe(onNext: {[weak self]isOn in
            guard let self = self else {return}
            ZFRealmManager.updateWithTranstion { bo in
                config.enableRecordCoordinate = isOn
            }
            self.subject.onNext(5)
        }).disposed(by: dis)
        
        elevationView.switchCell.switchBar.rx.isOn.changed.observe(on: MainScheduler.instance).subscribe(onNext: {[weak self]isOn in
            guard let self = self else {return}
            ZFRealmManager.updateWithTranstion { bo in
                config.enableElevation = isOn
            }
            self.subject.onNext(6)
        }).disposed(by: dis)
        
    }
    
}

/**
 地图工具
 */
class ZFMapToolView : ZFContentTitleView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setZFMapToolView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    fileprivate func setZFMapToolView(){
        self.titleCell.titleLabel.text = "工具"
        
        let cell = ZFContentCompassView()

        
        let cell1 = ZFSwitchCustomCell()
        cell1.imageView.isHidden = false
        cell1.titleLabel.text = "经纬度"
        cell1.imageView.image = R.image.ic_mine_intergel()
        cell1.imageView.snp.makeConstraints { make in
            make.width.height.equalTo(20)
        }
        let group = ZFGroupCustomCell(arrangedSubviews: [cell,cell1])
        contentView.addSubview(group)
        group.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(UIEdgeInsets(top: 5, left: 10, bottom: 5, right: 10))
        }

    }
    
}

/**
 图层
 */
class ZFMapLayerView :ZFContentTitleView {
    var sublayerView:ZFSubLayerView!
    override init(frame: CGRect) {
        super.init(frame: frame)
        setZFMapLayerView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    fileprivate func setZFMapLayerView(){
        titleCell.titleLabel.text = "工具"
        titleCell.titleLabel.text = "图层切换"
        titleCell.contentLabel.text = "更多"
        titleCell.contentLabel.font = UIFont.boldSystemFont(ofSize: 13)
        titleCell.arrow.isHidden = false
        
        sublayerView = ZFSubLayerView()
        contentView.addSubview(sublayerView)
        sublayerView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }

    }
}
